14. Exercise: Add EditText, Done Button, ClickHandler

ANDK L2 42 Click Handler SC

Android Developer Documentation:

In this exercise, do the following to finish out the AboutMe app:

  1. Add an EditText to get input for the nickname. Style with NameStyle.
  2. Add a hidden TextView for displaying the inputted text. Style with NameStyle.
  3. Add a Done button.
  4. Add a click handler to the Done button that displays the inputted text in the TextView and hides the EditText and button.

In your click handler:

  1. Find references to the nickname_edit and nickname_text views.
  2. Set the text of nickname_text to the value of nickname_edit:
    nicknameTextView.text = editText.text
  3. Update the visibility of the views. Use View.VISIBLE and View.GONE to set the visibility of the views.

In onCreate(), set a click handler like this:

findViewById<Button>(R.id.done_button).setOnClickListener {
   addNickname(it)
}

Hint: In your click handler, add this code to hide the keyboard after input is complete:

// Hide the keyboard.
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)

Here is what your finished app should look like.

If you want to start at this step, you can download this exercise code from: Step.04-Exercise-EditText-DoneButton-ClickHandler.

You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck, go back and watch the video again.

Once you’re done, you can check your solution against the solution we’ve provided here Step.04-Solution-EditText-DoneButton-ClickHandler or using this git diff

Task Description:

Check the steps below as you implement them to complete this exercise.

Task List:

Task Feedback:

Great, now go and show your AboutMe app to a friend!

Solution: Step.04-Solution-EditText-DoneButton-ClickHandler or diff